%初始化参数
N = 20;
d = 0.88;
r_min = 1.1;
r_max = 3;

%设置初始构型
%fname = sprintf('N=%d.txt',N-1);
fname = sprintf('N=%d.txt',13);
DATA = load(['.\SA_multiple\',fname]);
config_13 = cell(1,N);
for i=1:13
    config_13{i} = DATA(i+14,:);
end
config0 = create_config_multiple(config_13, r_max, r_min);

config_former = config0;
en_former = en_cal(config0);

%记录退火过程中目前最小能量对应的构型{1}和能量{2}
en_min = cell(1,2);
en_min{1} = config_former;
en_min{2} = en_former;

T_max = 2;
T_min = 1e-2;
lambda = 0.92;
step = 20000;
dr = 1;
choice = 1;
r_max = 3; r_min = d;
T = T_max;

while T > T_min
    for i = 1:step
        config = change_config_multiple(config_former,dr, r_max, r_min, choice);
        en = en_cal(config);
        if(en < en_former)
            config_former = config;
            en_former = en;
            if(en < en_min{2})
                en_min{1} = config;
                en_min{2} = en;
            end
        else
            rand_num = rand;
            factor = exp((en_former-en)/T);
            if(rand_num < factor)
                config_former = config;
                en_former = en;
            else

            end
        end
    end
    T = lambda * T;
    dr = lambda^(3/5) * dr;

end

config_former = en_min{1};
en_former = en_min{2};

T_max = 1e-2;
T_min = 1e-5;
lambda = 0.8;
step = 5000;
dr = 0.1;
choice = 1;
T = T_max;

while T > T_min
    for i = 1:step
        config = change_config_multiple(config_former,dr, r_max, r_min, choice);
        en = en_cal(config);
        if(en < en_former)
            config_former = config;
            en_former = en;
            if(en < en_min{2})
                en_min{1} = config;
                en_min{2} = en;
            end
        else
            rand_num = rand;
            factor = exp((en_former-en)/T);
            if(rand_num < factor)
                config_former = config;
                en_former = en;
            else

            end
        end
    end
    T = lambda * T;
    dr = lambda^(3/7) * dr;

end

%写.txt文件，存数据
fname = sprintf('N=%d.txt',N);
fid = fopen(['.\SA_multiple\',fname],'w');
for k = 1:N
    for m = 1:3
        fprintf(fid, '%.8f\t', config_former{k}(m));
    end
    fprintf(fid, '\n');
end
for m = 1:3
    fprintf(fid, '%.8f\t', en_former);
end
fprintf(fid, '\n');
for k = 1:N
    for m = 1:3
        fprintf(fid, '%.8f\t', en_min{1}{k}(m));
    end
    fprintf(fid, '\n');
end
for m = 1:3
    fprintf(fid, '%.8f\t', en_min{2});
end
fclose(fid);


